<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateGoodsAttributeRequest;
use App\Http\Requests\UpdateGoodsAttributeRequest;
use App\Repositories\GoodsAttributeRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class GoodsAttributeController extends AppBaseController
{
    /** @var  GoodsAttributeRepository */
    private $goodsAttributeRepository;

    public function __construct(GoodsAttributeRepository $goodsAttributeRepo)
    {
        $this->goodsAttributeRepository = $goodsAttributeRepo;
    }

    /**
     * Display a listing of the GoodsAttribute.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->goodsAttributeRepository->pushCriteria(new RequestCriteria($request));
        $goodsAttributes = $this->goodsAttributeRepository->paginate(10);

        return view('goods_attributes.index')
            ->with('goodsAttributes', $goodsAttributes);
    }

    /**
     * Show the form for creating a new GoodsAttribute.
     *
     * @return Response
     */
    public function create()
    {
        return view('goods_attributes.create');
    }

    /**
     * Store a newly created GoodsAttribute in storage.
     *
     * @param CreateGoodsAttributeRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsAttributeRequest $request)
    {
        $input = $request->all();

        $goodsAttribute = $this->goodsAttributeRepository->create($input);

        Flash::success('Goods Attribute saved successfully.');

        return redirect(route('goodsAttributes.index'));
    }

    /**
     * Display the specified GoodsAttribute.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            Flash::error('Goods Attribute not found');

            return redirect(route('goodsAttributes.index'));
        }

        return view('goods_attributes.show')->with('goodsAttribute', $goodsAttribute);
    }

    /**
     * Show the form for editing the specified GoodsAttribute.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            Flash::error('Goods Attribute not found');

            return redirect(route('goodsAttributes.index'));
        }

        return view('goods_attributes.edit')->with('goodsAttribute', $goodsAttribute);
    }

    /**
     * Update the specified GoodsAttribute in storage.
     *
     * @param  int              $id
     * @param UpdateGoodsAttributeRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsAttributeRequest $request)
    {
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            Flash::error('Goods Attribute not found');

            return redirect(route('goodsAttributes.index'));
        }

        $goodsAttribute = $this->goodsAttributeRepository->update($request->all(), $id);

        Flash::success('Goods Attribute updated successfully.');

        return redirect(route('goodsAttributes.index'));
    }

    /**
     * Remove the specified GoodsAttribute from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            Flash::error('Goods Attribute not found');

            return redirect(route('goodsAttributes.index'));
        }

        $this->goodsAttributeRepository->delete($id);

        Flash::success('Goods Attribute deleted successfully.');

        return redirect(route('goodsAttributes.index'));
    }
}
